﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using SafoverWpfFw;
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using static Safover.LidarCollector.ViewModel.DI;

namespace Safover.LidarCollector.ViewModel
{
    public class LoginPageViewModel : BaseViewModel
    {
        public bool LoginIsRunning { get; set; }

        public string UserName { get; set; }


        public ICommand LoginCommand { get; set; }

        public LoginPageViewModel()
        {
            // Create commands
            LoginCommand = new RelayParameterizedCommand(async (parameter) => await LoginAsync(parameter));
        }

        public async Task LoginAsync(object parameter)
        {
            await RunCommandAsync(() => LoginIsRunning, async () =>
            {
                await Task.Delay(1000);

                var secure = (parameter as IHavePassword).SecurePassword;

                IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(secure);

                var password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);

                if (UserName == "Admin" && password == "Admin")
                {
                    // TODO:
                }

                ViewModelApplication.HandleSuccessfulLogin();
            });
        }

    }
}
